package cn.study.vue_project_xxx.service.impl;

import cn.study.vue_project_xxx.common.constants.BaseConstants;
import cn.study.vue_project_xxx.common.exception.KnowException;
import cn.study.vue_project_xxx.entity.dto.RegisterUserDTO;
import cn.study.vue_project_xxx.entity.po.TAdmin;
import cn.study.vue_project_xxx.manager.TadminManager;
import cn.study.vue_project_xxx.manager.UserLoginManager;
import cn.study.vue_project_xxx.mapper.TAdminMapper;
import cn.study.vue_project_xxx.service.TAdminService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import static cn.study.vue_project_xxx.common.enumeration.MapKey.*;

/**
 * (TAdmin)表服务实现类
 *
 * @author makejava
 * @since 2023-04-25 09:53:15
 */
@Service("tAdminService")
public class TAdminServiceImpl extends ServiceImpl<TAdminMapper, TAdmin> implements TAdminService {

    @Resource
    TadminManager tadminManager;

    @Resource
    UserLoginManager userLoginManager;

    @Resource
    TAdminMapper adminMapper;

    @Override
    public Boolean addAdmin(RegisterUserDTO registerUserDTO) {
        HashMap<String, Integer> resultMap = tadminManager.saveEntity(registerUserDTO, BaseConstants.BaseNumberOne);
        // 返回保存结果
        return Objects.nonNull(resultMap.get(userInfoKey)) &&
                Objects.nonNull(resultMap.get(userLoginKey)) &&
                BaseConstants.BaseNumberTwo
                        .equals((resultMap.get(userInfoKey)+resultMap.get(userLoginKey)));
    }

    @Override
    public boolean deleteAdmin(List<Long> userIds) {
        //校验数据
        if (Objects.isNull(userIds)){
            throw new KnowException("无法得到要删除的数据！");
        }
        //执行删除
        HashMap<String, Integer> map = userLoginManager.deleteAdmin(userIds);
        return Objects.nonNull(map.get(userInfoKey)) &&
                Objects.nonNull(map.get(userLoginKey)) && map.get(userInfoKey)>=0;
    }

    @Override
    public boolean updateAdmin(TAdmin admin) {
        //校验数据
        if (Objects.isNull(admin)){
            throw new KnowException("参数缺失！");
        }
        if (admin.getAdminId()==null){
            throw new KnowException("无法获取登录者信息！");
        }
        HashMap<String, Integer> update = tadminManager.updateAdmin(admin);

        return Objects.nonNull(update.get(adminKey)) &&
                update.get(adminKey).equals(BaseConstants.BaseNumberOne);
    }

    @Override
    public Page<TAdmin> selectUsers(Page<TAdmin> page, List<Long> userIds) {
        //校验数据
        if (Objects.isNull(userIds)){
            throw new KnowException("无法得到要查询的数据！");
        }
        //执行查询
        return tadminManager.selectPage(page,userIds);
    }
}


